﻿using System;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class StatisticData_CPKStatistic_CPKbyPID_DT : System.Web.UI.Page
{
    private Customer cust;
    private ProductType pt;
    private TestItem_CPK cpk;
    private string var_productTypeID
    {
        get { return ViewState["var_productTypeID"].ToString(); }
        set { ViewState["var_productTypeID"] = value; }
    }
    private DateTime dt1
    {
        get { return DateTime.Parse(ViewState["dt1"].ToString()); }
        set { ViewState["dt1"] = value; }
    }
    private DateTime dt2
    {
        get { return DateTime.Parse(ViewState["dt2"].ToString()); }
        set { ViewState["dt2"] = value; }
    }

    public StatisticData_CPKStatistic_CPKbyPID_DT()
    {
        cust = new Customer();
        pt = new ProductType();
        cpk = new TestItem_CPK();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCustoms();
            BindProductTypes();
        }
    }

    protected void BindCustoms()
    {
        this.DropDownList1.DataSource = cust.LoadEntities();
        this.DropDownList1.DataTextField = "CustomerName";
        this.DropDownList1.DataValueField = "CustomerID";
        this.DropDownList1.DataBind();
    }

    protected void BindProductTypes()
    {
        this.DropDownList2.DataSource = pt.GetProductTypesbyCustomerID(this.DropDownList1.SelectedValue);
        this.DropDownList2.DataTextField = "ProductTypeName";
        this.DropDownList2.DataValueField = "ProductTypeID";
        this.DropDownList2.DataBind();
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindProductTypes();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        var_productTypeID = this.DropDownList2.SelectedValue;
        dt1 = DateTime.Parse(this.TextBox3.Text + " 00:00:00");
        dt2 = DateTime.Parse(this.TextBox4.Text + " 00:00:00");
        BindCPK(var_productTypeID, dt1, dt2);
    }

    protected void BindCPK(string var_productTypeID,DateTime dt1,DateTime dt2)
    {
        this.GridView1.DataSource = cpk.GetCPKbyPID_DT(var_productTypeID, dt1, dt2);
        this.GridView1.DataBind();
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            if (GridView1.Rows[i].Cells[1].Text == "-1.000")
                GridView1.Rows[i].Cells[1].Text = "-";
            if (GridView1.Rows[i].Cells[2].Text == "-1.000")
                GridView1.Rows[i].Cells[2].Text = "-";
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindCPK(var_productTypeID, dt1, dt2);
    }
}
